<?php get_main_menu("receptionist") ?>
    <?php if(!isset($_REQUEST['order_edit'])){ ?>
<script>
/*
This script is identical to the above JavaScript function.
*/
var ct = 1;
function new_link()
{
	ct++;
	var div1 = document.createElement('div');
	div1.id = ct;
	// link to delete extended form elements
	var delLink = '<a href="javascript:delIt('+ ct +')">Del</a>';
	div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
	document.getElementById('newlink').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId)
{
	d = document;
	var ele = d.getElementById(eleId);
	var parentEle = d.getElementById('newlink');
	parentEle.removeChild(ele);
}
</script>

<style>
   #newlink {width:100%}
</style>
<?php }?>
   <br/>
   <div style="width:90%; margin-left:auto; margin-right:auto;">
    <div class="form_title">

            <?php echo $_REQUEST["action"];?> Order <?php echo $row_get_branch_by_id['name']?>
    </div>
    <div class="box"> 
<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "order")) {
	
  $insertSQL = sprintf("INSERT INTO `order` (created_by, created_date, record_status, customer_id, branch_id, order_no) VALUES (%s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString(date("y-m-d H:i:s"), "date"),
                       GetSQLValueString($_POST['record_status'], "text"),
                       GetSQLValueString($_POST['customer_id'], "int"),
                       GetSQLValueString($_SESSION["branch_id"], "int"),
                       GetSQLValueString($_POST['order_no'], "text"));

  mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  $Result1 = mysql_query($insertSQL, $drycleaners_conn) or die(mysql_error());
  
    if(mysql_affected_rows($drycleaners_conn)>0){
		
	mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
	$query_get_order_last_insert = "SELECT * FROM drycleaners.order WHERE order.branch_id='".$_SESSION['branch_id']."' 
	ORDER BY order.id DESC LIMIT 0,1";
	$get_order_last_insert = mysql_query($query_get_order_last_insert, $drycleaners_conn) or die(mysql_error());
	$row_get_order_last_insert = mysql_fetch_assoc($get_order_last_insert);
	$totalRows_get_order_last_insert = mysql_num_rows($get_order_last_insert);
	
	if(count($_REQUEST['counter_id'])>0){
	$len = count($_POST['counter_id']);
		for ($i=0; $i <$len; $i++){
			echo "Item: ".$_REQUEST['item_id'][$i] . ' ';
			echo "TOS: ".$_REQUEST['type_of_service_id'][$i] . ' ';
			echo "Color: ".$_REQUEST['color'][$i] . ' ';		
			echo "Material: ".$_REQUEST['material_id'][$i] . '<br>';
			$insertSQL_items = sprintf("INSERT INTO order_item (item_id, color, material_id, type_of_service_id, order_id, created_date,created_by, record_status) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_REQUEST['item_id'][$i], "int"),
                       GetSQLValueString($_REQUEST['color'][$i], "text"),
                       GetSQLValueString($_REQUEST['material_id'][$i], "int"),
                       GetSQLValueString($_REQUEST['type_of_service_id'][$i], "int"),
                       GetSQLValueString($row_get_order_last_insert['id'], "int"),
					   GetSQLValueString(date("y-m-d H:i:s"), "date"),
					   GetSQLValueString($loggedin_id, "int"),
                       GetSQLValueString($_POST['record_status'], "text"));
				 mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
  				 $Result1 = mysql_query($insertSQL_items, $drycleaners_conn) or die(mysql_error());
			}
		 redirect_drycleaners("order_item_view&order_id=".$row_get_order_last_insert['id']."","2");
	}
	  redirect_drycleaners("order_item_view&order_id=".$row_get_order_last_insert['id']."","2");
}
}

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_customer = "SELECT person.first_name, person.last_name, customer.id
  FROM    drycleaners.customer customer
       INNER JOIN
          drycleaners.person person
       ON (customer.person_id = person.id)";
$customer = mysql_query($query_customer, $drycleaners_conn) or die(mysql_error());
$row_customer = mysql_fetch_assoc($customer);
$totalRows_customer = mysql_num_rows($customer);

if(isset($_REQUEST['order_edit'])){
$colname_order = "-1";
if (isset($_GET['id'])) {
  $colname_order = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order = sprintf("SELECT * FROM drycleaners.order WHERE id = %s", GetSQLValueString($colname_order, "int"));
$order = mysql_query($query_order, $drycleaners_conn) or die(mysql_error());
$row_order = mysql_fetch_assoc($order);
$totalRows_order = mysql_num_rows($order);
}


mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_last_insert = "SELECT * FROM drycleaners.order ORDER BY id DESC LIMIT 0 ,1";
$order_last_insert = mysql_query($query_order_last_insert, $drycleaners_conn) or die(mysql_error());
$row_order_last_insert = mysql_fetch_assoc($order_last_insert);
$totalRows_order_last_insert = mysql_num_rows($order_last_insert);


mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_item = "SELECT * FROM item";
$item = mysql_query($query_item, $drycleaners_conn) or die(mysql_error());
$row_item = mysql_fetch_assoc($item);
$totalRows_item = mysql_num_rows($item);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_type_of_service = "SELECT * FROM type_of_service";
$type_of_service = mysql_query($query_type_of_service, $drycleaners_conn) or die(mysql_error());
$row_type_of_service = mysql_fetch_assoc($type_of_service);
$totalRows_type_of_service = mysql_num_rows($type_of_service);

mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_material = "SELECT * FROM material";
$material = mysql_query($query_material, $drycleaners_conn) or die(mysql_error());
$row_material = mysql_fetch_assoc($material);
$totalRows_material = mysql_num_rows($material);


$colname_order_item_by_id = "-1";
if (isset($_GET['id'])) {
  $colname_order_item_by_id = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_by_id = sprintf("SELECT * FROM order_item WHERE id = %s", GetSQLValueString($colname_order_item_by_id, "int"));
$order_item_by_id = mysql_query($query_order_item_by_id, $drycleaners_conn) or die(mysql_error());
$row_order_item_by_id = mysql_fetch_assoc($order_item_by_id);
$totalRows_order_item_by_id = mysql_num_rows($order_item_by_id);

$colname_order_item_service = "-1";
if (isset($_GET['id'])) {
  $colname_order_item_service = $_GET['id'];
}
mysql_select_db($database_drycleaners_conn, $drycleaners_conn);
$query_order_item_service = sprintf("SELECT * FROM order_item_service WHERE order_item_id = %s", GetSQLValueString($colname_order_item_service, "int"));
$order_item_service = mysql_query($query_order_item_service, $drycleaners_conn) or die(mysql_error());
$row_order_item_service = mysql_fetch_assoc($order_item_service);
$totalRows_order_item_service = mysql_num_rows($order_item_service);

?>
<form action="<?php echo $editFormAction; ?>" method="post" name="order" id="order">
  <table align="left">
   <tr valign="baseline">
      <td nowrap="nowrap" align="left">Order No:</td>
      <td><input type="hidden" name="id" value="<?php echo $row_order['id']; ?>" size="32" />
      <input type="hidden" name="order_no" value="<?php if(isset($_REQUEST['order_edit'])){echo $row_order['order_no'];} else{ echo "Or".(1+$row_order_last_insert['id']);} ?>" size="32" /><?php if(isset($_REQUEST['order_edit'])){echo $row_order['order_no'];} else{ echo "Or".(1+$row_order_last_insert['id']);} ?>
      </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="left">Customer:</td>
      <td><select name="customer_id">
        <?php 
do {  
?>
        <option value="<?php echo $row_customer['id']?>" ><?php echo $row_customer['id']."".$row_customer['first_name']." ".$row_customer['last_name']?></option>
        <?php
} while ($row_customer = mysql_fetch_assoc($customer));
?>
      </select></td>
    </tr>
    <?php if(!isset($_REQUEST['order_edit'])){ ?>
    <tr>
    <td colspan="2">
 <p id="addnew">
	<a href="javascript:new_link()">Add More </a>
</p>
<div id="newlink">
<div>
<input type="hidden" name="counter_id[]" />
                 Item:<select name="item_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_item['id']?>"<?php if (!(strcmp($row_item['id'], $row_order_item_by_id['item_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_item['name']?></option>
                        <?php
                } while ($row_item = mysql_fetch_assoc($item));
                  $rows = mysql_num_rows($item);
                  if($rows > 0) {
                      mysql_data_seek($item, 0);
                      $row_item = mysql_fetch_assoc($item);
                  }
                ?>
                      </select>
                      
                     &nbsp; Material:<select name="material_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_material['id']?>"<?php if (!(strcmp($row_material['id'], $row_order_material_by_id['material_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_material['name']?></option>
                        <?php
                } while ($row_material = mysql_fetch_assoc($material));
                  $rows = mysql_num_rows($material);
                  if($rows > 0) {
                      mysql_data_seek($material, 0);
                      $row_material = mysql_fetch_assoc($material);
                  }
                ?>
                      </select>
                      &nbsp; Type Of Service
                      
                      <select name="type_of_service_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_type_of_service['id']?>"<?php if (!(strcmp($row_type_of_service['id'], $row_order_type_of_service_by_id['type_of_service_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_type_of_service['name']?></option>
                        <?php
                } while ($row_type_of_service = mysql_fetch_assoc($type_of_service));
                  $rows = mysql_num_rows($type_of_service);
                  if($rows > 0) {
                      mysql_data_seek($type_of_service, 0);
                      $row_type_of_service = mysql_fetch_assoc($type_of_service);
                  }
                ?>
                      </select>
                      &nbsp; Color:
                      <input type="text" name="color[]" value="<?php echo $row_order_item_by_id['color']; ?>" />
    
                      </select>
	</div>
 </div>

 </td>
 <td></td>
</tr>
 <?php }?>
    <tr valign="baseline">

      <td nowrap="nowrap" align="left" colspan="2">	  <?php if(isset($_REQUEST['order_edit'])){	
		echo"<input name='update' type='submit' value='UPDATE' id='submit'/>";
		echo" <a href='.?order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
		echo"<input type='hidden' name='MM_update' value='order_form' />";	
		}
		else{
			echo"<input name='save' type='submit' value='Continue' id='submit'/>";
			echo" <a href='.?order_view'><input name='add' type='button' value='Cancel' id='submit'/></a>";
			echo"<input type='hidden' name='MM_insert' value='order_form' />";	
			}
		?></td>
              <td >&nbsp;</td>
    </tr>
  </table>
  <input type="hidden" name="record_status" value="active" />
  <input type="hidden" name="MM_insert" value="order" />
</form>
 <div id="newlinktpl" style="display:none">
        <div>
        <input type="hidden" name="counter_id[]" />
        		   Item:<select name="item_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_item['id']?>"<?php if (!(strcmp($row_item['id'], $row_order_item_by_id['item_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_item['name']?></option>
                        <?php
                } while ($row_item = mysql_fetch_assoc($item));
                  $rows = mysql_num_rows($item);
                  if($rows > 0) {
                      mysql_data_seek($item, 0);
                      $row_item = mysql_fetch_assoc($item);
                  }
                ?>
                      </select>
                      
                     &nbsp; Material:<select name="material_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_material['id']?>"<?php if (!(strcmp($row_material['id'], $row_order_material_by_id['material_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_material['name']?></option>
                        <?php
                } while ($row_material = mysql_fetch_assoc($material));
                  $rows = mysql_num_rows($material);
                  if($rows > 0) {
                      mysql_data_seek($material, 0);
                      $row_material = mysql_fetch_assoc($material);
                  }
                ?>
                      </select>
                      &nbsp; Type Of Service
                      
                      <select name="type_of_service_id[]">
                        <?php
                do {  
                ?>
                        <option value="<?php echo $row_type_of_service['id']?>"<?php if (!(strcmp($row_type_of_service['id'], $row_order_type_of_service_by_id['type_of_service_id']))) {echo "selected=\"selected\"";} ?>><?php echo $row_type_of_service['name']?></option>
                        <?php
                } while ($row_type_of_service = mysql_fetch_assoc($type_of_service));
                  $rows = mysql_num_rows($type_of_service);
                  if($rows > 0) {
                      mysql_data_seek($type_of_service, 0);
                      $row_type_of_service = mysql_fetch_assoc($type_of_service);
                  }
                ?>
                      </select>
                      &nbsp; Color:
                      <input type="text" name="color[]" value="<?php echo $row_order_item_by_id['color']; ?>" />
                      </select>
        </div>
</div>
<?php
mysql_free_result($customer);
?>
</body>